package com.b511.swpu.cementbackend.config.sercurity;

import com.b511.swpu.cementbackend.entity.SysUser;
import com.b511.swpu.cementbackend.service.SysUserService;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.util.StringUtils;

public class TokenAuthenticationProvider implements AuthenticationProvider {

    private SysUserService sysUserService;

    public TokenAuthenticationProvider(SysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        if (null != authentication.getCredentials()) {
            String token = authentication.getCredentials().toString();
            String urlPath = authentication.getDetails().toString();
            authentication.setAuthenticated(sysUserService.userHasRole(token, urlPath));
        }
        return authentication;
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return SysUser.class.equals(authentication);
    }
}
